<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: IT宇宙人
 * Date: 2015-09-09
 */

namespace Home\Logic;

use Think\Model\RelationModel;
use Think\Page;

/**
 * 分类逻辑定义
 * Class CatsLogic
 * @package Home\Logic
 */
class UsersLogic extends RelationModel {
	/*
	 * 登陆
	 */
	public function login($username, $password) {
		$result = array();
		if (!$username || !$password)
			$result = array('status' => 0, 'msg' => '请填写账号或密码');

		$user = M('users') -> where("username='$username'") -> find();
		if (!$user) {
			$result = array('status' => -1, 'msg' => '账号不存在!');
		} elseif (md5($password) != $user['password']) {
			$result = array('status' => -2, 'msg' => '密码错误!');
		} elseif ($user['is_lock'] == 1) {
			$result = array('status' => -3, 'msg' => '账号异常已被锁定！！！');
		} else {
			//$_SERVER['HTTP_REFERER']
			$result = array('status' => 1, 'msg' => '登陆成功', 'url' => U('Home/Index/Index'));
			M('users') -> where("username='$username'") -> save(array("last_login" => time()));
			session('user', $user);

		}
		return $result;
	}

	/*
	 * 第三方登录
	 */
	public function thirdLogin($data = array()) {
		$openid = $data['openid'];
		//第三方返回唯一标识
		$oauth = $data['oauth'];
		//来源
		if (!$openid || !$oauth)
			return array('status' => -1, 'msg' => '参数有误', 'result' => '');
		//获取用户信息
		$user = get_user_info($openid, 3, $oauth);
		if (!$user) {
			//账户不存在 注册一个
			$map['password'] = '';
			$map['openid'] = $openid;
			$map['nickname'] = $data['nickname'];
			$map['reg_time'] = time();
			$map['oauth'] = $oauth;
			$map['head_pic'] = $data['head_pic'];
			$map['sex'] = $data['sex'];

			$map['first_leader'] = cookie('first_leader');
			// 推荐人id
			if ($_GET['first_leader'])
				$map['first_leader'] = $_GET['first_leader'];
			// 微信授权登录返回时 get 带着参数的

			// 如果找到他老爸还要找他爷爷他祖父等
			if ($map['first_leader']) {
				$first_leader = M('users') -> where("user_id = {$map['first_leader']}") -> find();
				$map['second_leader'] = $first_leader['first_leader'];
				$map['third_leader'] = $first_leader['second_leader'];
			} else {
				$map['first_leader'] = 0;
			}

			// 成为分销商条件
			$distribut_condition = tpCache('distribut.condition');
			if ($distribut_condition == 0)// 直接成为分销商, 每个人都可以做分销
				$map['is_distribut'] = 1;

			$row = M('users') -> add($map);
			$user = get_user_info($openid, 3, $oauth);
			// 会员注册送优惠券
			$coupon = M('coupon') -> where("send_end_time > " . time() . " and ((createnum - send_num) > 0 or createnum = 0) and type = 2") -> select();
			foreach ($coupon as $key => $val) {
				// 送券
				M('coupon_list') -> add(array('cid' => $val['id'], 'type' => $val['type'], 'uid' => $user[user_id], 'send_time' => time()));
				M('Coupon') -> where("id = {$val['id']}") -> setInc('send_num');
				// 优惠券领取数量加一
			}

		}
		return array('status' => 1, 'msg' => '登陆成功', 'result' => $user);
	}

	/**
	 * 注册
	 * @param $username  邮箱或手机
	 * @param $password  密码
	 * @param $password2 确认密码
	 * @return array
	 */
	public function register($username, $password) {

		$data['username'] = $username;
		$data['password'] = md5($password);
		$data['reg_time'] = time();
		$user = M('users');
		$ret = $user -> where("username = '$username'") -> find();
		if (!empty($ret)) {
			$back['status'] = -1;
			$back['msg'] = '用户名存在';
			return $back;
		}

		$id = $user -> add($data);

		if ($id) {
			$ret = $user -> where("user_id = $id") -> find();
			$back['status'] = 1;
			$back['result'] = $ret;
			$back['msg'] = '注册成功';
			$back['url'] = U('Home/User/ucenter');
			//设置session
			session('user', $ret);

			return $back;

		}
		$back['status'] = 0;
		$back['msg'] = '注册失败';
		return $back;

	}

	/**
	 * 个人资料数据获取
	 * @param $username  邮箱或手机
	 * @param $password  密码
	 * @param $password2 确认密码
	 * @return array
	 */
	public function ucenter() {
		$user = session('user');
		$data['loginname'] = $user['username'];
		$data['email'] = $user['email'];
		$data['mobile'] = $user['mobile'];
		$data['realname'] = $user['realname'];
		$data['nickname'] = $user['nickname'];
		return $data;

	}

	/*
	 * 个人资料修改
	 */
	public function ucenter_modify($data) {
		$result = array();
		$user = session('user');
		$user_id = $user['user_id'];
		$users = M('users');

		$flag = $users -> where(array('user_id' => $user_id)) -> save($data);
		if ($flag !== false) {
			$result = array('status' => 1, 'msg' => '保存成功', 'url' => U('ucenter'));
		} else {
			$result = array('status' => -1, 'msg' => '保存失败');
		}

		return $result;
	}

	/**
	 * 我的订单数据获取
	 * @param $username  邮箱或手机
	 * @param $password  密码
	 * @param $password2 确认密码
	 * @return array
	 */
	public function myorder() {
		$orders = M('orders');
		$list = $orders -> order('order_id desc') -> select();
		return $list;

	}

	/*
	 * 获取账户资金记录
	 */
	public function get_account_log($user_id, $type = 0) {
		//查询条件
		//        $type = I('get.type',0);
		if ($type == 1) {
			//收入
			$where = 'user_money > 0 OR pay_points > 0 AND user_id=' . $user_id;
		}
		if ($type == 2) {
			//支出
			$where = 'user_money < 0 OR pay_points < 0 AND user_id=' . $user_id;
		}

		$count = M('account_log') -> where($where ? $where : 'user_id = ' . $user_id) -> count();
		$Page = new Page($count, 16);
		$logs = M('account_log') -> where($where ? $where : 'user_id = ' . $user_id) -> order('change_time desc') -> limit($Page -> firstRow . ',' . $Page -> listRows) -> select();

		$return['status'] = 1;
		$return['msg'] = '';
		$return['result'] = $logs;
		$return['show'] = $Page -> show();

		return $return;
	}

	/**
	 * 获取评论列表
	 * @param $user_id 用户id
	 * @param $status  状态 0 未评论 1 已评论
	 * @return mixed
	 */
	public function get_comment($user_id, $status = 2) {
		if ($status == 1) {
			//已评论
			$count2 = M('') -> query("select count(*) as count from `__PREFIX__comment`  as c inner join __PREFIX__order_goods as g on c.goods_id = g.goods_id and c.order_id = g.order_id where c.user_id = $user_id");
			$count2 = $count2[0]['count'];

			$page = new Page($count2, 10);
			$sql = "select c.*,g.*,(select order_sn from  __PREFIX__order where order_id = c.order_id ) as order_sn  from  __PREFIX__comment as c inner join __PREFIX__order_goods as g on c.goods_id = g.goods_id and c.order_id = g.order_id where c.user_id = $user_id order by c.add_time desc LIMIT {$page->firstRow},{$page->listRows}";
		} else {
			$countsql = " select count(1) as comment_count from __PREFIX__order_goods as og
        	left join __PREFIX__order as o on o.order_id = og.order_id where o.user_id = $user_id  and og.is_send = 1 ";
			$where = '';
			if ($status == 0) {
				$countsql .= $where = " and og.is_comment = 0 ";
			}
			$comment = M() -> query($countsql);
			$count1 = $comment[0][comment_count];
			// 待评价
			$page = new Page($count1, 3);
			$sql = " select *  from __PREFIX__order_goods as og 
            left join __PREFIX__order as o on o.order_id = og.order_id  where o.user_id = $user_id and og.is_send = 1  
            $where order by o.order_id desc  LIMIT {$page->firstRow},{$page->listRows}";
		}

		$show = $page -> show();
		$comment_list = M() -> query($sql);
		if ($comment_list) {
			$return['result'] = $comment_list;
			$return['show'] = $show;
			//分页
			return $return;
		} else {
			return array();
		}
	}

	/**
	 * 添加评论
	 * @param $order_id  订单id
	 * @param $goods_id  商品id
	 * @param $user_email用户邮箱地址
	 * @param $username  用户名
	 * @return bool
	 */
	public function add_comment($add) {
		if (!$add[order_id] || !$add[goods_id])
			return array('status' => -1, 'msg' => '非法操作', 'result' => '');

		//检查订单是否已完成
		$order = M('order') -> where("order_id = {$add[order_id]} AND user_id = {$add[user_id]}") -> find();
		if ($order['order_status'] != 2)
			return array('status' => -1, 'msg' => '该笔订单还未完成', 'result' => '');

		//检查是否已评论过
		$goods = M('comment') -> where("order_id = {$add[order_id]} AND goods_id = {$add[goods_id]}") -> find();
		if ($goods)
			return array('status' => -1, 'msg' => '您已经评论过该商品', 'result' => '');

		$row = M('comment') -> add($add);
		if ($row) {
			//更新订单商品表状态
			M('order_goods') -> where(array('goods_id' => $add[goods_id], 'order_id' => $add[order_id])) -> save(array('is_comment' => 1));
			M('goods') -> where(array('goods_id' => $add[goods_id])) -> setInc('comment_count', 1);
			// 评论数加一
			// 查看这个订单是否全部已经评论,如果全部评论了 修改整个订单评论状态
			$comment_count = M('order_goods') -> where("order_id ='{$add[order_id]}' and is_comment = 0") -> count();
			if ($comment_count == 0)// 如果所有的商品都已经评价了 订单状态改成已评价
			{
				M('order') -> where("order_id ='{$add[order_id]}'") -> save(array('order_status' => 4));
			}
			return array('status' => 1, 'msg' => '评论成功', 'result' => '');
		}
		return array('status' => -1, 'msg' => '评论失败', 'result' => '');
	}

	/**
	 * 邮箱或手机绑定
	 * @param $email_mobile  邮箱或者手机
	 * @param int $type  1 为更新邮箱模式  2 手机
	 * @param int $user_id  用户id
	 * @return bool
	 */
	public function update_email_mobile($email_mobile, $user_id, $type = 1) {
		//检查是否存在邮件
		if ($type == 1)
			$field = 'email';
		if ($type == 2)
			$field = 'mobile';
		$condition['user_id'] = array('neq', $user_id);
		$condition[$field] = $email_mobile;

		$is_exist = M('users') -> where($condition) -> find();
		if ($is_exist)
			return false;
		unset($condition[$field]);
		$condition['user_id'] = $user_id;
		$validate = $field . '_validated';
		M('users') -> where($condition) -> save(array($field => $email_mobile, $validate => 1));
		return true;
	}

	/**
	 * 更新用户信息
	 * @param $user_id
	 * @param $post  要更新的信息
	 * @return bool
	 */
	public function update_info($user_id, $post = array()) {
		$model = M('users') -> where("user_id = " . $user_id);
		$row = $model -> save($post);
		if ($row === false)
			return false;
		return true;
	}

	/**
	 * 地址添加/编辑
	 * @param $user_id 用户id
	 * @param $user_id 地址id(编辑时需传入)
	 * @return array
	 */
	public function add_address($user_id, $address_id = 0, $data) {
		$post = $data;
		if ($address_id == 0) {
			$c = M('UserAddress') -> where("user_id = $user_id") -> count();
			if ($c >= 20)
				return array('status' => -1, 'msg' => '最多只能添加20个收货地址', 'result' => '');
		}

		//检查手机格式
		if ($post['consignee'] == '')
			return array('status' => -1, 'msg' => '收货人不能为空', 'result' => '');
		if (!$post['province'] || !$post['city'] || !$post['district'])
			return array('status' => -1, 'msg' => '所在地区不能为空', 'result' => '');
		if (!$post['address'])
			return array('status' => -1, 'msg' => '地址不能为空', 'result' => '');
		if (!check_mobile($post['mobile']))
			return array('status' => -1, 'msg' => '手机号码格式有误', 'result' => '');

		//编辑模式
		if ($address_id > 0) {

			$address = M('user_address') -> where(array('address_id' => $address_id, 'user_id' => $user_id)) -> find();
			if ($post['is_default'] == 1 && $address['is_default'] != 1)
				M('user_address') -> where(array('user_id' => $user_id)) -> save(array('is_default' => 0));
			$row = M('user_address') -> where(array('address_id' => $address_id, 'user_id' => $user_id)) -> save($post);
			if (!$row)
				return array('status' => -1, 'msg' => '操作完成', 'result' => '');
			return array('status' => 1, 'msg' => '编辑成功', 'result' => '');
		}
		//添加模式
		$post['user_id'] = $user_id;

		// 如果目前只有一个收货地址则改为默认收货地址
		$c = M('user_address') -> where("user_id = {$post['user_id']}") -> count();
		if ($c == 0)
			$post['is_default'] = 1;

		$address_id = M('user_address') -> add($post);
		//如果设为默认地址
		$insert_id = M() -> getLastInsID();
		$map['user_id'] = $user_id;
		$map['address_id'] = array('neq', $insert_id);

		if ($post['is_default'] == 1)
			M('user_address') -> where($map) -> save(array('is_default' => 0));
		if (!$address_id)
			return array('status' => -1, 'msg' => '添加失败', 'result' => '');

		return array('status' => 1, 'msg' => '添加成功', 'result' => $address_id);
	}

	/**
	 * 设置默认收货地址
	 * @param $user_id
	 * @param $address_id
	 */
	public function set_default($user_id, $address_id) {
		M('user_address') -> where(array('user_id' => $user_id)) -> save(array('is_default' => 0));
		//改变以前的默认地址地址状态
		$row = M('user_address') -> where(array('user_id' => $user_id, 'address_id' => $address_id)) -> save(array('is_default' => 1));
		if (!$row)
			return false;
		return true;
	}

	/**
	 * 修改密码
	 * @param $user_id  用户id
	 * @param $old_password  旧密码
	 * @param $new_password  新密码
	 * @param $confirm_password 确认新 密码
	 */
	public function password($user_id, $old_password, $new_password, $confirm_password, $is_update = true) {
		$data = $this -> get_info($user_id);
		$user = $data['result'];
		if (strlen($new_password) < 6)
			return array('status' => -1, 'msg' => '密码不能低于6位字符', 'result' => '');
		if ($new_password != $confirm_password)
			return array('status' => -1, 'msg' => '两次密码输入不一致', 'result' => '');
		//验证原密码
		if ($is_update && ($user['password'] != '' && encrypt($old_password) != $user['password']))
			return array('status' => -1, 'msg' => '密码验证失败', 'result' => '');
		$row = M('users') -> where("user_id='{$user_id}'") -> save(array('password' => encrypt($new_password)));
		if (!$row)
			return array('status' => -1, 'msg' => '修改失败', 'result' => '');
		return array('status' => 1, 'msg' => '修改成功', 'result' => '');
	}

	/**
	 * 取消订单
	 */
	public function cancel_order($user_id, $order_id) {
		$order = M('order') -> where(array('order_id' => $order_id, 'user_id' => $user_id)) -> find();
		//检查是否未支付订单 已支付联系客服处理退款
		if (empty($order))
			return array('status' => -1, 'msg' => '订单不存在', 'result' => '');
		//检查是否未支付的订单
		if ($order['pay_status'] > 0 || $order['order_status'] > 0)
			return array('status' => -1, 'msg' => '支付状态或订单状态不允许', 'result' => '');
		//获取记录表信息
		//$log = M('account_log')->where(array('order_id'=>$order_id))->find();
		//有余额支付的情况
		if ($order['user_money'] > 0 || $order['integral'] > 0) {
			accountLog($user_id, $order['user_money'], $order['integral'], "订单取消，退回{$order['user_money']}元,{$order['integral']}积分");
		}

		$row = M('order') -> where(array('order_id' => $order_id, 'user_id' => $user_id)) -> save(array('order_status' => 3));

		$data['order_id'] = $order_id;
		$data['action_user'] = $user_id;
		$data['action_note'] = '您取消了订单';
		$data['order_status'] = 3;
		$data['pay_status'] = $order['pay_status'];
		$data['shipping_status'] = $order['shipping_status'];
		$data['log_time'] = time();
		$data['status_desc'] = '用户取消订单';
		M('order_action') -> add($data);
		//订单操作记录

		if (!$row)
			return array('status' => -1, 'msg' => '操作失败', 'result' => '');
		return array('status' => 1, 'msg' => '操作成功', 'result' => '');

	}

	/**
	 * app发送短信验证码记录
	 * @param $mobile
	 * @param $code
	 * @param $session_id
	 */
	public function sms_log($mobile, $code, $session_id) {
		//判断是否存在验证码
		$data = M('sms_log') -> where(array('mobile' => $mobile, 'session_id' => $session_id)) -> order('id DESC') -> find();
		//获取时间配置
		$sms_time_out = tpCache('sms.sms_time_out');
		$sms_time_out = $sms_time_out ? $sms_time_out : 120;
		//120秒以内不可重复发送
		if ($data && (time() - $data['add_time']) < $sms_time_out)
			return array('status' => -1, 'msg' => $sms_time_out . '秒内不允许重复发送');
		$row = M('sms_log') -> add(array('mobile' => $mobile, 'code' => $code, 'add_time' => time(), 'session_id' => $session_id));
		if (!$row)
			return array('status' => -1, 'msg' => '发送失败');
		//$send = sendSMS($mobile,'您好，你的验证码是：'.$code);
		$send = sendSMS($mobile, $code);
		return array('status' => 1, 'msg' => '发送成功');
		if (!$send)
			return array('status' => -1, 'msg' => '发送失败');
		return array('status' => 1, 'msg' => '发送成功');
	}

	/**
	 * 短信验证码验证
	 * @param $mobile   手机
	 * @param $code  验证码
	 * @param $session_id   唯一标示
	 * @return bool
	 */
	public function sms_code_verify($mobile, $code, $session_id) {
		//判断是否存在验证码
		$data = M('sms_log') -> where(array('mobile' => $mobile, 'session_id' => $session_id, 'code' => $code)) -> order('id DESC') -> find();
		if (empty($data))
			return array('status' => -1, 'msg' => '手机验证码不匹配');

		//获取时间配置
		$sms_time_out = tpCache('sms.sms_time_out');
		$sms_time_out = $sms_time_out ? $sms_time_out : 120;
		//验证是否过时
		if ((time() - $data['add_time']) > $sms_time_out)
			return array('status' => -1, 'msg' => '手机验证码超时');
		//超时处理
		M('sms_log') -> where(array('mobile' => $mobile, 'session_id' => $session_id, 'code' => $code)) -> delete();
		return array('status' => 1, 'msg' => '验证成功');
	}

	/**
	 * 发送验证码
	 * @param $sender 接收人
	 * @param $type 发送类型
	 * @return json
	 */
	public function send_validate_code($sender, $type) {
		$sms_time_out = tpCache('sms.sms_time_out');
		$sms_time_out = $sms_time_out ? $sms_time_out : 180;
		//获取上一次的发送时间
		$send = session('validate_code');
		if (!empty($send) && $send['time'] > time() && $send['sender'] == $sender) {
			//在有效期范围内 相同号码不再发送
			$res = array('status' => -1, 'msg' => '规定时间内,不要重复发送验证码');
		}
		$code = mt_rand(1000, 9999);
		if ($type == 'email') {
			//检查是否邮箱格式
			if (!check_email($sender))
				$res = array('status' => -1, 'msg' => '邮箱码格式有误');
			$send = send_email($sender, '验证码', '您好，你的验证码是：' . $code);
		} else {
			if (!check_mobile($sender))
				$res = array('status' => -1, 'msg' => '手机号码格式有误');
			//$send = sendSMS($sender,'您好，你的验证码是：'.$code);
			$send = sendSMS($sender, $code);
		}
		if ($send) {
			$info['code'] = $code;
			$info['sender'] = $sender;
			$check['is_check'] = 0;
			$info['time'] = time() + $sms_time_out;
			//有效验证时间
			session('validate_code', $info);
			$res = array('status' => 1, 'msg' => '验证码已发送，请注意查收');
		} else {
			$res = array('status' => -1, 'msg' => '验证码发送失败,请检查');
		}
		exit(json_encode($res));
	}

	public function check_validate_code($code, $sender) {
		$check = session('validate_code');
		if (empty($check)) {
			$res = array('status' => 0, 'msg' => '请先获取验证码');
		} elseif ($check['time'] < time()) {
			$res = array('status' => 0, 'msg' => '验证码已超时失效');
		} elseif ($code != $check['code'] || $check['sender'] != $sender) {
			$res = array('status' => 0, 'msg' => '验证失败,验证码有误');
		} else {
			$check['is_check'] = 1;
			//标示验证通过
			session('validate_code', $check);
			$res = array('status' => 1, 'msg' => '验证成功');
		}
		exit(json_encode($res));
	}

	//	编辑旅游路线

	public function publish_edit($id, $type) {
		$user = session('user');

		$data['title'] = I('post.title', '');
		$data['price'] = I('post.price', '');

		$data['content'] = htmlspecialchars(I('post.content', ''));
		$data['spec'] = I('post.spec', '');

		//车辆
		if ($type == 1) {
			$data['pickplace'] = I('post.pickplace', '');
			$data['returnplace'] = I('post.returnplace', '');
			$data['cartype'] = I('post.cartype', '');
			//			$data['startplace'] = I('post.startplace');
		}
		//线路
		if ($type == 2) {

			$data['desc'] = I('post.desc', '');
			$data['travelday'] = I('post.travelday', '');
			$data['startplace'] = I('post.startplace', '');
		}

		$data['logo_pic'] = I('post.logo', '');

		$ret = M('publish') -> where('id=' . $id) -> save($data);
		if ($ret !== FALSE) {
			$back['status'] = 1;
			$back['msg'] = '更新旅游路线成功';

		} else {

			$back['status'] = -1;
			$back['msg'] = '更新旅游路线失败';
		}
		return $back;

	}

	//	发布旅游路线
	public function publish_submit($type) {
		$user = session('user');
		$data['title'] = I('post.title', '');
		$data['price'] = I('post.price', '');
		$data['spec'] = I('post.spec', '');

		$data['content'] = I('post.content', '');
		//车辆
		if ($type == 1) {
			$data['pickplace'] = I('post.pickplace', '');
			$data['returnplace'] = I('post.returnplace', '');
			$data['cartype'] = I('post.cartype', '');
			//			$data['startplace'] = I('post.startplace');
		}
		//线路
		if ($type == 2) {

			$data['desc'] = I('post.desc', '');
			$data['travelday'] = I('post.travelday', '');
			$data['startplace'] = I('post.startplace', '');
		}

		$data['type'] = $type;
		$data['logo_pic'] = I('post.logo', '');
		$data['userid'] = $user['user_id'];

		$data['createtime'] = time();
		$data['pid'] = $this -> build_order_no();
		$data['status'] = 1;

		$ret = $publish = M('publish') -> add($data);
		if ($ret) {
			$back['status'] = 1;
			$back['msg'] = '发布旅游路线成功';

		} else {

			$back['status'] = -1;
			$back['msg'] = '发布旅游路线失败';
		}
		return $back;

	}

	public function build_order_no() {
		return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
	}

}
